﻿using System;
using System.Web.Security;
using System.Collections.Generic;

using DflyingForum.Core.Data.Domain.Entities;
using DflyingForum.Core.Framework;
using DflyingForum.Core.Data.NHHelper;

using NHibernate;
using DflyingForum.Core.Data.Domain.ValueObjects;
using System.Web;


namespace DflyingForum.Core.Repositories
{
    public class UserReposity : NHibernateRepository<User, int>
    {

        #region 用户安全与验证

        /// <summary>
        /// 用户是否登录
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static bool CheckLogIn()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 设置用户登录验证票据
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpassword"></param>
        /// <param name="isPersistant">是否持久，若持久有效期为三个月</param>
        public void SetLogCookie(string username, bool isPersistant = false)
        {
            User user = this.FindBy("UserName", username);
            IList<Group> groups = user.Groups;
            string groupStr = string.Empty;
            foreach (var item in groups)
            {
                groupStr += string.IsNullOrEmpty(groupStr) ? item.ID.ToString() : ("," + item.ID.ToString());
            }
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1,
                username,
                DateTime.Now,
                DateTime.Now.AddMinutes(30),
                isPersistant,
                groupStr//存放组
                );

            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            if (isPersistant)//持久，有效期为三个月
                DflyingCookie.SetCookie(FormsAuthentication.FormsCookieName, encryptedTicket, DateTime.Now.AddMonths(3));
            else//临时
                DflyingCookie.SetCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        }

        /// <summary>
        /// 注销
        /// </summary>
        public static void SignOut()
        {
            //使原始验证Cookie过期
            DflyingCookie.SetCookie(FormsAuthentication.FormsCookieName, "", DateTime.Now.AddYears(-3));
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// 跳转到登陆前请求的页面
        /// </summary>
        public static void RedirectFromLoginPage(string username, bool isPersistant = false)
        {
            FormsAuthentication.RedirectFromLoginPage(username, isPersistant);
        }

        /// <summary>
        /// 根据用户名和密码验证是否存在指定用户
        /// </summary>
        public bool IsExistUser(string username, string password)
        {
            User u = FindBy("UserName", username);
            if (u == null)
                return false;
            if (string.Equals(u.UserPwd, password))
            {
                return true;
            }
            return false;
        }

        #endregion


        #region 用户扩展方法

        /// <summary>
        /// 获取某板块的某个角色的用户
        /// </summary>
        public IList<User> GetModuleManager(int moduleId, int flag)
        {
            return _session.GetNamedQuery("GetModuleManager")
                .SetInt32("moduleId", moduleId)
                .SetInt32("flag", flag)
                .List<User>();
        }

        #endregion


        #region 用户权限管理

        /// <summary>
        /// 返回用户的管理权限
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public IList<UnitRight> GetUnitRights(User user)
        {
            //ICriterion  是表达式
            //ICriteria 查询方法
            string hql = "select g from User u inner join u.Groups g";
            return null;
        }

        #endregion

    }
}